package com.jellyfishumbrella.GL;

import org.lwjgl.Sys;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;

/**
 * TODO Put here a description of what this class does.
 * 
 * @author 500441547. Created Sep 4, 2012.
 */
public abstract class AbstractKeyboardMouse extends ThreadLoop {
	public static AbstractGameLogic	logic;
	public static RendererGL		renderGL;
	public long						currTime;
	public static long				startTime			= 0;
	protected static long			timerTicksPerSecond	= Sys.getTimerResolution();
	public static long				lastKeyTime			= 0;
	protected static final long		keyDelayMillis		= 300;

	public AbstractKeyboardMouse(String name, RendererGL renderGL) {
		super(name);
		this.renderGL = renderGL;
	}

	public void process() {
		currTime = AbstractGameLogic.getTime();
		keyboard();
		mouse();
	}

	public void keyboard() {
		// by default, handles standard WASD movement,
		// with C and V controlling camera altitude
		if (Keyboard.isKeyDown(Keyboard.KEY_LSHIFT) || Keyboard.isKeyDown(Keyboard.KEY_RSHIFT)) {
			renderGL.camera.CREEP = true;
		} else {
			renderGL.camera.CREEP = false;
		}
		if (Keyboard.isKeyDown(Keyboard.KEY_W)) {
			renderGL.camera.moveForward(currTime + 1);
		}
		if (Keyboard.isKeyDown(Keyboard.KEY_A)) {
			renderGL.camera.moveLeft(currTime + 1);
		}
		if (Keyboard.isKeyDown(Keyboard.KEY_S)) {
			renderGL.camera.moveBackward(currTime + 1);
		}
		if (Keyboard.isKeyDown(Keyboard.KEY_D)) {
			renderGL.camera.moveRight(currTime + 1);
		}
		if (Keyboard.isKeyDown(Keyboard.KEY_C)) {
			renderGL.camera.moveDown(currTime + 1);
		}
		if (Keyboard.isKeyDown(Keyboard.KEY_V)) {
			renderGL.camera.moveUp(currTime + 1);
		}
	}

	public void mouse() {
		// by default, handles basic camera pointing
		int notches = Mouse.getDWheel();
		float FOV;

		if (notches < 0) {
			FOV = (float) Math.min(179.0, renderGL.camera.FOV * 1.1);
			renderGL.camera.FOV = FOV;
			renderGL.doUpdate = true;
		} else if (notches > 0) {
			FOV = (float) Math.max(0.0, renderGL.camera.FOV / 1.1);
			renderGL.camera.FOV = FOV;
			renderGL.doUpdate = true;
		}

		int mx = Mouse.getX(), my = Mouse.getY();
		float diffX = Mouse.getDX() * renderGL.camera.FOV * 0.1f / 50.0f;// 0.1f;
																			// x*0.1/FOV;
		float diffY = Mouse.getDY() * renderGL.camera.FOV * 0.1f / 50.0f;// 0.1f;
																			// x*0.1/FOV;
		renderGL.camera.rotXoffset(-diffY, currTime + 1);
		renderGL.camera.rotYoffset(diffX, currTime + 1);
		/*
		 * // Selection Magic if (Mouse.isButtonDown(0) && !lastMouseDown) { mx
		 * = renderGL.DISPLAY_WIDTH / 2; my = renderGL.DISPLAY_HEIGHT / 2;
		 * Vector3f worldPos = renderGL.screenTo3D(mx, my);//
		 * DISPLAY_WIDTH/2,DISPLAY_HEIGHT/2); } lastMouseDown =
		 * Mouse.isButtonDown(0);
		 */
	}
}
